package com.css.zfzx.sjcj.modules.hddcwyGeochemicalSvyLine.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.css.zfzx.sjcj.modules.hddcwyGeochemicalSvyLine.repository.HddcWyGeochemicalsvylineNativeRepository;
import com.css.zfzx.sjcj.modules.hddcwyGeochemicalSvyLine.repository.HddcWyGeochemicalsvylineRepository;
import com.css.zfzx.sjcj.modules.hddcwyGeochemicalSvyLine.repository.entity.HddcWyGeochemicalsvylineEntity;
import com.css.zfzx.sjcj.modules.hddcwyGeochemicalSvyLine.service.HddcWyGeochemicalsvylineService;
import com.css.zfzx.sjcj.modules.hddcwyGeochemicalSvyLine.viewobjects.HddcWyGeochemicalsvylineQueryParams;
import com.css.zfzx.sjcj.modules.hddcwyGeochemicalSvyLine.viewobjects.HddcWyGeochemicalsvylineVO;
import com.css.bpm.platform.api.local.PlatformAPI;
import com.css.bpm.platform.sys.dict.repository.entity.DictItemEntity;
import com.css.bpm.platform.utils.*;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @author zhangcong
 * @date 2020-12-02
 */
@Service
public class HddcWyGeochemicalsvylineServiceImpl implements HddcWyGeochemicalsvylineService {

	@Autowired
    private HddcWyGeochemicalsvylineRepository hddcWyGeochemicalsvylineRepository;
    @Autowired
    private HddcWyGeochemicalsvylineNativeRepository hddcWyGeochemicalsvylineNativeRepository;

    @Override
    public JSONObject queryHddcWyGeochemicalsvylines(HddcWyGeochemicalsvylineQueryParams queryParams , int curPage,
                                                     int pageSize) {
        Page<HddcWyGeochemicalsvylineEntity> hddcWyGeochemicalsvylinePage =
                this.hddcWyGeochemicalsvylineNativeRepository.queryHddcWyGeochemicalsvylines(queryParams , curPage, pageSize);
        JSONObject jsonObject = PlatformPageUtils.formatPageData(hddcWyGeochemicalsvylinePage);
        return jsonObject;
    }


    @Override
    public HddcWyGeochemicalsvylineEntity getHddcWyGeochemicalsvyline(String id) {
        HddcWyGeochemicalsvylineEntity hddcWyGeochemicalsvyline = this.hddcWyGeochemicalsvylineRepository.findById(id).orElse(null);
         return hddcWyGeochemicalsvyline;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public HddcWyGeochemicalsvylineEntity saveHddcWyGeochemicalsvyline(HddcWyGeochemicalsvylineEntity hddcWyGeochemicalsvyline) {
        String uuid = UUIDGenerator.getUUID();
        hddcWyGeochemicalsvyline.setId(uuid);
        hddcWyGeochemicalsvyline.setCreateUser(PlatformSessionUtils.getUserId());
        hddcWyGeochemicalsvyline.setCreateTime(PlatformDateUtils.getCurrentTimestamp());
        this.hddcWyGeochemicalsvylineRepository.save(hddcWyGeochemicalsvyline);
        return hddcWyGeochemicalsvyline;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public HddcWyGeochemicalsvylineEntity updateHddcWyGeochemicalsvyline(HddcWyGeochemicalsvylineEntity hddcWyGeochemicalsvyline) {
        hddcWyGeochemicalsvyline.setUpdateUser(PlatformSessionUtils.getUserId());
        hddcWyGeochemicalsvyline.setUpdateTime(PlatformDateUtils.getCurrentTimestamp());
        this.hddcWyGeochemicalsvylineRepository.save(hddcWyGeochemicalsvyline);
        return hddcWyGeochemicalsvyline;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteHddcWyGeochemicalsvylines(List<String> ids) {
        List<HddcWyGeochemicalsvylineEntity> hddcWyGeochemicalsvylineList = this.hddcWyGeochemicalsvylineRepository.findAllById(ids);
        if(!PlatformObjectUtils.isEmpty(hddcWyGeochemicalsvylineList) && hddcWyGeochemicalsvylineList.size() > 0) {
            for(HddcWyGeochemicalsvylineEntity hddcWyGeochemicalsvyline : hddcWyGeochemicalsvylineList) {
                this.hddcWyGeochemicalsvylineRepository.delete(hddcWyGeochemicalsvyline);
            }
        }
    }

    @Override
    public List<DictItemEntity> getValidDictItemsByDictCode(String dictCode) {
        List<DictItemEntity> validDictItems = PlatformAPI.getDictAPI().getValidDictItemsByDictCode(dictCode);
        return validDictItems;
    }

}
